<%--
  Created by IntelliJ IDEA.
  User: 平步青云
  Date: 2021/4/26
  Time: 16:56
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="ZH-CN">
<%@include file="/WEB-INF/include-head.jsp" %>
<link rel="stylesheet" href="css/pagination.css">
<script src = "crowd-js/my-role.js"></script>
<script src = "jquery/jquery.pagination.js"></script>
<script type="text/javascript">
    $(function(){
        //1.对分页操作准备初始化数据
        window.pageNum = 1;
        window.pageSize = 5;
        window.keyword = "";
        //2.调用执行分页的函数
        generatePage();
        $("#searchBtn").click(function(){
                window.keyword = $("#keywordInput").val();
                generatePage();
        });
        //点击新增按钮打开模态框
        $("#showAddModelBtn").click(function(){
           $("#AddRoleModel").modal("show")
        });
        //
        $("#saveRoleBtn").click(function(){
           //获取用户输入的名称
            //#AddRoleModle表示找到整个模态框
            //空格表示在后代元素中继续查找
            //[name = roleName]表示匹配name属性等于roleName的元素
            var roleName= $.trim($("#AddRoleModel [name = roleName]").val());
            //发送Ajax请求
            $.ajax({
                "url":"role/save.json",
                "type":"post",
                "data":{
                    "name" : roleName
                },
                "dataType":"json",
                "success":function(response){
                    var result = response.result;
                    if(result == "SUCCESS"){
                        layer.msg("操作成功");
                        //将页码重新定位到最后一页
                        window.pageNum = 999999;
                        //重新加载分页
                        generatePage();
                    }
                    if(result == "FAILED"){
                        layer.msg("操作失败"+response.message);
                    }
                },
                "error":function(response){
                    layer.msg(response.stuts+" "+response.stutsText);
                }
            });
            //关闭模态框
            $("#AddRoleModel").modal("hide")
            //清理模态框
            $("#AddRoleModel [name=roleName]").val("");
        });

        //修改按钮绑定点击事件,弹出模态框
        //使用jQuery对象的on()函数可以解决上面问题
        $("#rolePageBody").on("click",".pencilBtn",function(){
            $("#EditRoleModel").modal("show");

            //通过DOM方式获取表格中当前行中的 角色名称
            var roleName = $(this).parent().prev().text();
            //获取当前角色Id
            //依据是我们吧roleid设置到修改的id 属性中
             window.roleId = this.id;
            //把roleName的值设置模态框中的文本框
            $("#EditRoleModel [name = roleName]").val(roleName);
        });
        //给更新模态框的更新绑定单机事件
        $("#updateRoleBtn").click(function(){
            $.ajax({
                "url":"role/update.json",
                "type":"post",
                "data":{
                    "id" : window.roleId,
                    "name":$("#EditRoleModel [name = roleName]").val(),
                },
                "dataType":"json",
                "success":function(response){
                    var result = response.result;
                    if(result == "SUCCESS"){
                        layer.msg("操作成功");
                        //重新加载分页
                        generatePage();
                    }
                    if(result == "FAILED"){
                        layer.msg("操作失败"+response.message);
                    }
                },
                "error":function(response){
                    layer.msg(response.stuts+" "+response.stutsText);
                }
            });
            $("#EditRoleModel").modal("hide");
        });
        //点击确认模态框中的确认删除按钮执行删除
        $("#removeRoleBtn").click(function(){
            var roleIdList = JSON.stringify(window.roleIdArray);

            console.log(roleIdList);
            console.log(window.roleIdArray);
            $.ajax({
                "url" :"/role/remove/by/role/id/array.json",
                "type":"post",
                "data":roleIdList,
                "contentType":"application/json;charset=UTF-8",
                "dataType":"json",
                "success":function(response){
                    var result = response.result;
                    if(result == "SUCCESS"){
                        layer.msg("操作成功");
                        //重新加载分页
                        generatePage();
                    }
                    if(result == "FAILED"){
                        layer.msg("操作失败"+response.message);
                    }
                },
                "error":function(response){
                    layer.msg(response.stuts+" "+response.stutsText);
                }
            });
            $("confirmModel").modal("hide");
        });
        $("#rolePageBody").on("click",".removeBtn",function(){
            //从当前按钮出发获取角色名称
            var roleName = $(this).parent().prev().text();
            var roleId = this.id;
            //创建role对象存入数组
            var roleArray = [{
                roleId:roleId,
                roleName:roleName
            }]
            showConfirmModel(roleArray);
        });
        $("#summaryBox").click(function(){
            //获取当前多选框自身的状态
            var currentStatus = this.checked;
            //用当前多选框的状态设置其他多选框
            $(".itemBox").prop("checked",currentStatus);
        });
        //全选
        $("#rolePageBody").on("click",".itemBox",function(){
            //获取当前已经选中的itemBox 数量
            var checkedBoxCount = $(".itemBox:checked").length;
            //获取全部的itemBox的数量
            var totalBoxCount  = $(".itemBox").length;
            //
            $("#summaryBox").prop("checked",checkedBoxCount == totalBoxCount);
        });
        //批量删除的按钮绑定函数
        $("#batchRemoveBtn").click(function(){
            var roleArray = [];
            //遍历当前选中的多选框
            $(".itemBox:checked").each(function(){
                var roleId = this.id;
                //通过DOM操作获得角色名称
                var roleName= $(this).parent().next().text();
                roleArray.push({
                    "roleId" : roleId,
                    "roleName":roleName
                });
            });
            //检查roleArray的长度是否为零
            if(roleArray.length == 0){
                layer.msg("请至少选择一个执行删除");
                return;
            }
            //调用专门的函数打开模态框
            showConfirmModel(roleArray);
        });
        //声明专门的函数显示确认模态框
        function showConfirmModel(roleArray){
            console.log(roleArray);
            //打开模态框
            $("#confirmModel").modal("show");
            //清除旧数据
            $("#roleNameDiv").empty();
            //在全局变量范围创建数组用来存放角色id
            window.roleIdArray = [];
            //遍历roleArray数组
            for(var i = 0 ;i<roleArray.length;i++){
                var role = roleArray[i];
                var roleName = role.roleName;
                $("#roleNameDiv").append(role.roleId+"</br>");
                $("#roleNameDiv").append(roleName+"</br>");
                //调用数组对象的push方法,存入新的元素
                var roleId = role.roleId;
                window.roleIdArray.push(roleId);
            }
        }

    });
</script>
<body>
<%@include file="/WEB-INF/include-nav.jsp" %>
<div class="container-fluid">
    <div class="row">
        <%@include file="/WEB-INF/include-sidebar.jsp" %>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><i class="glyphicon glyphicon-th"></i> 数据列表</h3>
                </div>
                <div class="panel-body">
                    <form class="form-inline" role="form" style="float:left;">
                        <div class="form-group has-feedback">
                            <div class="input-group">
                                <div class="input-group-addon">查询条件</div>
                                <input name="keyword" id = "keywordInput" class="form-control has-success" type="text" placeholder="请输入查询条件">
                            </div>
                        </div>
                        <button type="button" id = "searchBtn" class="btn btn-warning"><i class="glyphicon glyphicon-search"></i> 查询</button>
                    </form>
                    <button type="button" id = "batchRemoveBtn" class="btn btn-danger" style="float:right;margin-left:10px;"><i class=" glyphicon glyphicon-remove"></i> 删除</button>
                    <button type="button" id = "showAddModelBtn" class="btn btn-primary" style="float:right;"><i class="glyphicon glyphicon-plus"></i> 新增</button>
                    <br>
                    <hr style="clear:both;">
                    <div class="table-responsive">
                        <table class="table  table-bordered">
                            <thead>
                            <tr>
                                <th width="30">编号</th>
                                <th width="30"><input id="summaryBox"  type="checkbox"></th>
                                <th>名称</th>
                                <th width="100">操作</th>
                            </tr>
                            </thead>
                            <tbody id="rolePageBody">
                            <tr>
                                <td>1</td>
                                <td><input type="checkbox"></td>
                                <td>PM - 项目经理</td>
                                <td>
                                    <button type="button" class="btn btn-success btn-xs"><i class=" glyphicon glyphicon-check"></i></button>
                                    <button type="button" class="btn btn-primary btn-xs"><i class=" glyphicon glyphicon-pencil"></i></button>
                                    <button type="button" class="btn btn-danger btn-xs"><i class=" glyphicon glyphicon-remove"></i></button>
                                </td>
                            </tr>
                            </tbody>
                            <tfoot>
                            <tr>
                                <td colspan="4" align="center">
                                    <div id = "Pagination" class="pagination">
                                        <!--这里显示分页-->
                                    </div>
                                </td>
                            </tr>

                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<%@include file="/WEB-INF/model-role-add.jsp"%>
<%@include file="/WEB-INF/model-role-edit.jsp"%>
<%@include file="/WEB-INF/model-role-confirm.jsp"%>
</body>
</html>

